package com.ease.web.xwt.component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 
 * @author nathanleewei
 * 
 */
public class XForm extends XAbstractComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8643296288592309446L;

	public static final int COLSPAN = 4;

	private int colspan = COLSPAN;

	private List<XFormPair> xFormPairs;

	private List<String> notNullFields;

	private List<String> rows;

	protected String getTemplate() {
		return TEMPLATE_X_FORM;
	}

	public String generate() {
		this.buildRows();
		return super.generate();
	}

	public void addNotNullField(String fieldName) {
		if (null == notNullFields) {
			notNullFields = new ArrayList<String>();

		}
		notNullFields.add(fieldName);
	}

	private void sortPair() {
		if (null != xFormPairs) {
			Collections.sort(xFormPairs, new Comparator<XFormPair>() {
				public int compare(XFormPair object0, XFormPair object1) {
					return object0.getOrderIndex() - object1.getOrderIndex();
				}
			});
		}
	}

	public XForm addXFormPair(XFormPair xFormPair) {
		if (null == xFormPairs) {
			xFormPairs = new ArrayList<XFormPair>();
		}
		xFormPairs.add(xFormPair);
		return this;
	}

	public void buildRows() {

		if (null != xFormPairs) {
			sortPair();
			//
			rows = new ArrayList<String>();
			int needCols = 0;
			int restCols = colspan;
			StringBuffer cells = new StringBuffer();
			//
			XFormPair xFormPair = null;
			for (int i = 0; i < xFormPairs.size(); i++) {
				xFormPair = (XFormPair) xFormPairs.get(i);
				if (xFormPair.isVisible()) {
					needCols = xFormPair.getColSpan() + 1;
					if (needCols > colspan) {
						needCols = colspan;
					}
					if (restCols == 0) {
						rows.add(cells.toString());
						cells = new StringBuffer();
						cells.append(xFormPair.generate());
						restCols = colspan - needCols;
					} else if (restCols >= needCols) {
						cells.append(xFormPair.generate());
						restCols = restCols - needCols;
					} else {
						if (restCols > 0) {
							cells.append(XFormPair.emptyCell(restCols));
							rows.add(cells.toString());
							cells = new StringBuffer();
							restCols = colspan;
						}
					}
				}
			}
			if (restCols == 0 && !"".equals(cells.toString())) {
				rows.add(cells.toString());
				cells = new StringBuffer();
				cells.append(xFormPair.generate());
			}
			if (restCols > 0 && restCols < colspan) {
				cells.append(XFormPair.emptyCell(restCols));
				rows.add(cells.toString());
			}
			cells = null;
		}
	}

	// setter/getter//////////////

	/**
	 * @return Returns the colspan.
	 */
	public int getColspan() {
		return colspan;
	}

	/**
	 * @param colspan
	 *            The colspan to set.
	 */
	public void setColspan(int colspan) {
		this.colspan = colspan;
	}

	/**
	 * @return Returns the width.
	 */
	public String getWidth() {
		return width;
	}

	/**
	 * @param width
	 *            The width to set.
	 */
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @return Returns the xFormPairs.
	 */
	public List<XFormPair> getXFormPairs() {
		return xFormPairs;
	}

	/**
	 * @param formPairs
	 *            The xFormPairs to set.
	 */
	public void setXFormPairs(List<XFormPair> formPairs) {
		xFormPairs = formPairs;
	}

	/**
	 * @return Returns the rows.
	 */
	public List<String> getRows() {
		return rows;
	}

	/**
	 * @param rows
	 *            The rows to set.
	 */
	public void setRows(List<String> rows) {
		this.rows = rows;
	}

	/**
	 * @return Returns the notNullFields.
	 */
	public List<String> getNotNullFields() {
		return notNullFields;
	}

	/**
	 * @param notNullFields
	 *            The notNullFields to set.
	 */
	public void setNotNullFields(List<String> notNullFields) {
		this.notNullFields = notNullFields;
	}

}
